<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Register</title>
        <script type="text/javascript">
            function confirm() {

                var error_msg = "";
                //check username
                if (form1.txtUserName.value == "")
                    error_msg += "* User name is mandatory\n";
                else if (!/^[a-z0-9]+$/gi.test(form1.txtUserName.value)) {
                    error_msg += "* Only alphanumeric is allowed in User name\n";
                }

                //check email
                if (form1.txtEmail.value == "")
                    error_msg += "* Email is mandatory\n";
                else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/gi.test(form1.txtEmail.value)) {
                    error_msg += "* Ivalid format at Email\n";
                }

                //check password
                if (form1.txtPassword.value == "")
                    error_msg += "* Password is mandatory\n";
                else if (!(/[\d]/g.test(form1.txtPassword.value) && /[a-z]/gi.test(form1.txtPassword.value))) {
                    error_msg += "* Password must contains both letters and numbers\n";
                }

                //check re-password
                if (form1.txtPassword.value != form1.txtRePassword.value)
                    error_msg += "* Password and Re-type password do not match\n";
                //check fullname
                if (form1.txtFullName.value == "")
                    error_msg += "* Full name is mandatory\n";
                else if (form1.txtFullName.value.length <= 6) {
                    error_msg += "* Full name must have more than 6 characters\n";
                }

                //check birthday
                if (form1.txtBirthday.value != "") {
                    var birthday_valid = true;
                    try {
                        var dateparts = form1.txtBirthday.value.split("/");
                        if (dateparts.length != 3) {
                            birthday_valid = false;
                        } else if (dateparts[0].length != 2 || dateparts[1].length != 2 || dateparts[2].length != 4) {
                            birthday_valid = false;
                        } else {
                            var birthday = parseInt(dateparts[0]);
                            var birthmonth = parseInt(dateparts[1]);
                            var birthyear = parseInt(dateparts[2]);
                            if (birthday < 1 || birthday > 31 || birthmonth < 1 || birthmonth > 12 || birthyear < 1900 || birthyear > 2010) {
                                birthday_valid = false;
                            }
                            else {
                                switch (birthmonth) {
                                    case 2:
                                        if ((birthyear % 4 == 0 && birthyear % 100 != 0) || (birthyear % 400 == 0)) {
                                            if (birthday > 29 || birthday < 1) {
                                                birthday_valid = false;
                                            }
                                        }
                                        else {
                                            if (birthday > 28 || birthday < 1) {
                                                birthday_valid = false;
                                            }
                                        }
                                        break;

                                    case 1:
                                    case 3:
                                    case 5:
                                    case 7:
                                    case 8:
                                    case 10:
                                    case 12:
                                        if (birthday > 31 || birthday < 1) {
                                            birthday_valid = false;
                                        }
                                        break;

                                    default:
                                        if (birthday > 30 || birthday < 1) {
                                            birthday_valid = false;
                                        }
                                }

                            }
                        }
                    } catch (e) {
                        birthday_valid = false;
                    }

                    if (!birthday_valid)
                        error_msg += "* Ivalid datetime format in Birthday\n";
                }

                //check phone
                if (form1.txtPhone.value != "" && !/[\d]{1,3}-[\d]{1,3}-[\d]{7}/g.test(form1.txtPhone.value)) {
                    error_msg += "* Invalid phone number format\n"
                }

                //check department
                if (form1.cboDepartments.value == "none") {
                    error_msg += "* Please! choice department\n"
                }

                //alert
                if (error_msg != "") {
                    alert("Please fix the following errors:\n" + error_msg);
                    return false;
                }
                return true;
            }
        </script>
    </head>
    <?php
    $submit = $_REQUEST["btnSubmit"];
    if (!empty($submit)) {
        //get info
        $user_name = $_REQUEST["txtUserName"];
        $email = $_REQUEST["txtEmail"];
        $password = md5($_REQUEST["txtRePassword"]);
        $full_name = $_REQUEST["txtFullName"];
        $birthday = $_REQUEST["txtBirthday"];
        $phone = $_REQUEST["txtPhone"];
        $country = $_REQUEST["cboCountries"];
        $department = $_REQUEST["cboDepartments"];

        //connect database
        include "ConnectDb.php";

        //check user name unique
        $query_user_name = "SELECT user_name FROM users WHERE user_name = '$user_name'";
        $result_user_name = mysql_query($query_user_name, $mysql);
        $row_user_name = mysql_fetch_row($result_user_name);

        //check email unique
        $query_email = "SELECT email FROM users WHERE email = '$email'";
        $result_email = mysql_query($query_email);
        $row_email = mysql_fetch_row($result_email);

        if (!empty($row_user_name) || !empty($row_email)) {
            if (!empty($row_user_name)) {
                $notice_user_name = "User name already in use";
            }
            if (!empty($row_email)) {
                $notice_email = "Email already in use";
            }
            //persisted country
            switch ($country) {
                case "none":
                    $select_none = "selected='selected'";
                    break;
                case "1":
                    $select_usa = "selected='selected'";
                    break;
                case "2":
                    $select_england = "selected='selected'";
                    break;
                case "3":
                    $select_thailand = "selected='selected'";
                    break;
                case "4":
                    $select_vietnam = "selected='selected'";
                    break;
                case "5":
                    $select_other = "selected='selected'";
                    break;
            }

            //persisted department
            switch ($department) {
                case "none":
                    $select_none = "selected='selected'";
                    break;
                case "1":
                    $select_1 = "selected='selected'";
                    break;
                case "2":
                    $select_2 = "selected='selected'";
                    break;
                case "3":
                    $select_3 = "selected='selected'";
                    break;
                case "4":
                    $select_4 = "selected='selected'";
                    break;
                case "5":
                    $select_5 = "selected='selected'";
                    break;
            }
        }
        // insert info to myQQL
        else {
            //format birthday to SQL
            if (!empty($birthday)) {
                $array_ddmmyyyy = explode("/", $birthday);
                $birthday = "$array_ddmmyyyy[2]-$array_ddmmyyyy[1]-$array_ddmmyyyy[0]";
            } else {
                $birthday = NULL;
            }

            //format country to SQL
            if ($country == "none") {
                $country = 5;
            }

            //format phone to SQL
            if (empty($phone)) {
                $phone = NULL;
            }

            //insert
            $query = "INSERT INTO users (user_name, email, password, full_name, birthday, phone, country_id, department_id, signup_date) "
                    . "VALUE ('$user_name', '$email', '$password', '$full_name', '$birthday', '$phone', '$country', '$department', now())";
            mysql_query($query, $mysql);

            //jump page
            header("Location: SuccessfullyRegistered.php");
        }
    }
    ?>
    <body>
        <a href="Register.php">Register</a> | <a href="Login.php">Login</a>
        <hr />
        <form name="form1" action="" onSubmit="return confirm();">
            User name: <input name="txtUserName" type="text" value="<?php echo $user_name; ?>"/><span> <?php echo $notice_user_name; ?></span><br />
            Email: <input name="txtEmail" type="text" value="<?php echo $email; ?>"/><span> <?php echo $notice_email; ?></span><br />
            Password: <input name="txtPassword" type="password" /><br />
            Re-type password: <input name="txtRePassword" type="password" /><br />
            Full name: <input name="txtFullName" type="text" value="<?php echo $full_name; ?>"><br />
                Birthday (dd/mm/yyyy): <input name="txtBirthday" type="text" value="<?php echo $birthday; ?>"/><br />
                Phone (&lt;country_code&gt;-&lt;area_code&gt;-&lt;phone_number&gt;): <input name="txtPhone" type="text" value="<?php echo $phone; ?>"/><br />
                Country: <select name="cboCountries">
                    <option <?php echo $select_none; ?> >none</option>
                    <option value="1" <?php echo $select_usa; ?> >USA</option>
                    <option value="2" <?php echo $select_england; ?> >England</option>
                    <option value="3" <?php echo $select_thailand; ?> >Thailand</option>
                    <option value="4" <?php echo $select_vietnam; ?> >Vietnam</option>
                    <option value="5" <?php echo $select_other; ?> >Other</option>	
                </select><br />
                Department: <select name="cboDepartments">
                    <option>none</option>
                    <option value="1" <?php echo $select_1; ?> >Products</option>
                    <option value="2" <?php echo $select_2; ?> >Out-sourcing</option>
                    <option value="3" <?php echo $select_3; ?> >Custom-Works</option>
                    <option value="4" <?php echo $select_4; ?> >Design</option>
                    <option value="5" <?php echo $select_5; ?> >Finance</option>	
                </select><br />
                <input type="submit" value="Submit" name="btnSubmit" /> <input type="reset" value="Cancel" name="btnCancel" />
        </form>
    </body>
</html>
